<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use redis\RedisPackage;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class FamemberpushController extends Controller
{

    /**
     * 推送唤醒
     * User:@hang
     */
    public function index(){
        $push_type = input('push_type');
        $push_member_id = input('push_member_id');
        $push_content = input('push_content');
        if(empty($push_member_id)){
            return $this->fetch();
        }
        if(empty($push_type)){
            return admin_error('请选择推送类型',URL_CLOSE_REFRESH);
        }
        if(empty($push_content)){
            return admin_error('请输入推送内容',URL_CLOSE_REFRESH);
        }
        //操作上锁
        $Redis_Lock  =  new \Redislock();
        $otc_sell_key  =  'lock:famemberpush';	// 定义锁标识
        $is_lock  =  $Redis_Lock->lock($otc_sell_key, 15);	// 获取锁
        //解锁失败
        if(!$is_lock){
            return admin_error('操作频繁，请稍后再试！',URL_CLOSE_REFRESH);
        }
        $IndexTableModel = New IndexTableModel();
        $pushMemberInfo = $IndexTableModel->getFind('fa_member',"`member_id` = '{$push_member_id}'",'member_id,me_sex,me_status');
        if(empty($pushMemberInfo)){
            return admin_error('推送人用户信息查询失败',URL_CLOSE_REFRESH);
        }
        if($push_type == 1){
            //使用【女用户】推送给【平台男用户】
            if($pushMemberInfo['me_sex'] != 2){
                return admin_error('您选择的是推送给【平台男用户】，请使用女性用户推送',URL_CLOSE_REFRESH);
            }
        }else{
            //使用【男用户】推送给【平台女用户】
            if($pushMemberInfo['me_sex'] != 1){
                return admin_error('您选择的是推送给【平台女用户】，请使用男性用户推送',URL_CLOSE_REFRESH);
            }
        }
        if($pushMemberInfo['me_status'] != 1){
            return admin_error('抱歉，推送用户已经被封禁',URL_CLOSE_REFRESH);
        }
        //查询符合的被推送人
        $ctime = time();
        $ttime = $ctime - 7 * 86400;

        if($push_type == 1){
            //使用【女用户】推送给【平台男用户】
            $list = Db::table("fa_member")->where("member_id > 52 and me_zx_time >= $ttime and me_sex = 1 and me_status = 1 and me_parent not in(4226,4230,4409,4360,4548)")->order('member_id desc')->column('member_id');
        }else{
            //使用【男用户】推送给【平台女用户】
            $list = Db::table("fa_member")->where("member_id > 52 and me_zx_time >= $ttime and me_sex = 2 and me_status = 1")->order('member_id desc')->column('member_id');
        }
//        $Redis_Lock->unlock($otc_sell_key);	//释放锁
//        var_dump($list);die;
        if(empty($list)){
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_error('没有符合可推送的人员',URL_CLOSE_REFRESH);
        }
//        $list = [16,17,18];//测试
        $memberArr = array();
        if(count($list) > 800){
            //分成几个数组
            $user_count = "20"; //分成几组
            $group_num = 800; //每组多少个
            $memberArr = $this->array_group($list,$user_count,$group_num);
        }else{
            $memberArr[0] = $list;
        }
        $redis = New RedisPackage();
        foreach ($memberArr as $k=>$v){
            if(!empty($v)){
                $memberStr = implode(',',$v);
                $redis->lpush("pushmemberlist",$push_member_id.'Z'.$memberStr.'Z'.$push_content);
            }
        }
        $Redis_Lock->unlock($otc_sell_key);	//释放锁
        return admin_success('推送执行中，预计1分钟内全部推送完成', URL_RELOAD);
    }


    /*
    $arrF：数组
    $user_count：分成几组
    $group_num：每组多少个
    */
    public function array_group($arrF,$user_count,$group_num){
        for($i=0;$i<$user_count;$i++){
            if($i == $user_count-1){
                $arrT[] = array_slice($arrF, $i * $group_num );
            }else{
                $arrT[] = array_slice($arrF, $i * $group_num ,$group_num);
            }
        }
        return $arrT;
    }



}
